package week08.company02;

/**
 * Created by zhangxiaokun on 2018/3/21.
 */
public class ComMediator extends Mediator {

    @Override
    public void execute(String type, Object... objs) {
        //采购电脑，  销售电脑，  折价销售电脑，  清仓处理
        if (type.equalsIgnoreCase("purchase.buy")) {
            this.buyIBMComputer( (Integer)objs[0]);

        } else if (type.equalsIgnoreCase("sale.sell")) {
            this.saleComputer( (Integer)objs[0]);

        } else if (type.equalsIgnoreCase("sale.offSale")) {
            this.offSell();

        } else if (type.equalsIgnoreCase("stock.clear")) {
            this.clearStock();
        }
    }

    //清库存
    public void clearStock() {
        super.stock.clearStock();   //清空仓库中所有电脑
    }

    //采购电脑
    public void buyIBMComputer(int number) {
        //销量》80， 销量好
        if (super.sale.getSaleStatus() > 80) {
            super.purchase.buyIBMComputer(number);  //买电脑
            super.stock.increase(number);           //加库存量
            System.out.println("采购电脑" + number + "台");

            //销量《80， 销量不好,折半采购
        } else {
            int buyNum = number / 2;
            super.purchase.buyIBMComputer(buyNum);
            super.stock.increase(buyNum);
            System.out.println("采购电脑" + number + "台");
        }
    }

    //销售电脑
    public void saleComputer(int number) {
        //库存不够
        if (super.stock.getStock() < number) {
            super.purchase.buyIBMComputer(number);
//            super.stock.decrease(number);

        //库存量足够
        } else {
            super.stock.decrease(number);
        }
    }

    //折半销售电脑
    public void offSell() {
        ///////////////////////////////////////
        System.out.println("打折销售" + super.stock.getStock() + "台电脑");
        super.stock.clearStock();
    }
}
